package com.jinlejia.erp.ui.mine.contract;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityAuditAgreementDetailsBinding;
import com.jinlejia.erp.ui.common.CallUpDialog;
import com.jinlejia.erp.ui.common.ProgressDialog;
import com.jinlejia.erp.utils.CallUtils;
import com.jinlejia.erp.utils.DataFilter;
import com.jinlejia.erp.utils.StringUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

/**
 * 审核 - 补充协议详情(activity)
 */

public class SupplementAgreementDetailsActivity extends BaseActivity {

    private CallUpDialog dialog;
    private String Customer_phone_number, owner_phone_number, rscode, cscode, chose;
    private int state = -1;
    private ArrayList<String> rejectReasonList;
    private ProgressDialog progressDialog;

    private ActivityAuditAgreementDetailsBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_audit_agreement_details);
        binding.setEvent(this);
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        progressDialog = new ProgressDialog(this, "加载中...");
        getData();
    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:                  //返回
                finish();
                break;
            case R.id.contact_details_link1:      //客户电话
                dialog = new CallUpDialog(SupplementAgreementDetailsActivity.this, Customer_phone_number);
                dialog.setConfirmListener(() -> callTask(Customer_phone_number));
                dialog.show();
                break;
            case R.id.contact_details_link2:      //业主电话
                dialog = new CallUpDialog(SupplementAgreementDetailsActivity.this, owner_phone_number);
                dialog.setConfirmListener(() -> callTask(owner_phone_number));
                dialog.show();
                break;
            case R.id.supplement_text:
                //查看补充协议
                Intent intent = new Intent(SupplementAgreementDetailsActivity.this, CheckSupplementAgreementActivity.class);
                intent.putExtra("id", getIntent().getStringExtra("id"));
                startActivity(intent);
                break;
            case R.id.audit_btn:
                //审核
                Intent intent1 = new Intent(SupplementAgreementDetailsActivity.this, AuditSupplementAgreementActivity.class);
                intent1.putExtra("id", getIntent().getStringExtra("id"));
                intent1.putStringArrayListExtra("rejectReasonList", rejectReasonList);
                startActivity(intent1);
                break;
            default:
                break;
        }
    }

    /**
     * 获取数据
     */
    private void getData() {
        progressDialog.show();
        OkHttpUtils
                .get()
                .url(ApiConfig.SUPPLY_AGREEMENT_DETAIL)
                .addParams("supplyAgreementId", getIntent().getStringExtra("id"))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(SupplementAgreementDetailsActivity.this, response, null)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            binding.contactDetailsNumber.setText(data.getString("agreementCode"));
                            binding.contactDetailsPassengerNumber.setText(data.getString("csCode"));
                            if (!TextUtils.isEmpty(data.getString("csCode")))
                                cscode = data.get("csCode").toString();
                            binding.detailsPassengerName.setText(data.getString("customerName"));
                            binding.contactDetailsHouseNumber.setText(data.getString("rscode"));
                            if (!TextUtils.isEmpty(data.getString("rscode")))
                                rscode = data.get("rscode").toString();
                            binding.detailsHouseName.setText(data.getString("owner"));
                            binding.contactDetailsPeople.setText(data.getString("tradeUserName"));
                            binding.contactDetailsAddress.setText(data.getString("shareStoreName"));
                            binding.contactDetailsPropertyAddress.setText(data.getString("address"));
                            switch (data.getString("contractType")) {
                                case "0":
                                    chose = "sale";
                                    binding.contactDetailsState.setText("买卖");
                                    binding.contactDetailsMoney.setText(StringUtil.valueOf(data.get("tradePrice")).concat("万"));
                                    break;
                                case "1":
                                    chose = "rent";
                                    binding.contactDetailsMoney.setText(StringUtil.valueOf(data.get("rentPrice")).concat("元"));
                                    binding.contactDetailsState.setText("租赁");
                                    break;
                                case "2":
                                    chose = "sale";
                                    binding.contactDetailsState.setText("一手房");
                                    binding.contactDetailsMoney.setText(StringUtil.valueOf(data.get("tradePrice")).concat("万"));
                                    break;
                                case "3":
                                    chose = "sale";
                                    binding.contactDetailsState.setText("垫资");
                                    binding.contactDetailsMoney.setText(data.getString("tradePrice").concat("万"));
                                    break;
                                default:
                                    break;
                            }
                            if (!TextUtils.isEmpty(data.getString("csTel"))) {
                                Customer_phone_number = data.get("csTel").toString();
                                state = 0;
                            }
                            if (!TextUtils.isEmpty(data.getString("rsTel"))) {
                                owner_phone_number = data.get("rsTel").toString();
                                state = 1;
                            }
                            rejectReasonList = JSON.parseObject(data.getJSONArray("rejectReasonList").toJSONString(), new TypeReference<ArrayList<String>>() {
                            });
                        }
                        progressDialog.dismiss();
                    }
                });
    }

    public void callTask(String phone) {
        CallUtils.CallTel(SupplementAgreementDetailsActivity.this, phone);
    }

}
